package com.hotau.location;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.hotau.HotauActivity;
import com.hotau.R;
import com.hotau.util.PreferencesUtils;

/**
 * 说明界面
 * 
 * @author hanshengliang
 * @data 2011-12-10
 * @version 1.0
 *          <ul>
 *          <li>1.0 创建</li>
 *          </ul>
 */
public class InstructionFragment extends Fragment implements
		OnItemSelectedListener, OnItemClickListener {
	public static final String TAG = "InstructionFragment";
	private Gallery mInstructionGallery;
	private ImageAdapter mAdapter;
	private static final int[] mPicture = new int[] { R.drawable.instruction01,
			R.drawable.instruction02, R.drawable.instruction03 };

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		mInstructionGallery = (Gallery) getActivity().findViewById(
				R.id.pre_instruction_gallery);
		mInstructionGallery.setOnItemClickListener(this);
		mInstructionGallery.setOnItemSelectedListener(this);
		mInstructionGallery.setLongClickable(false);
		mAdapter = new ImageAdapter(getActivity());
		mInstructionGallery.setAdapter(mAdapter);
		mInstructionGallery.setSelection(0);
		mAdapter.notifyDataSetChanged();
		PreferencesUtils.setInstruction(getActivity(),
				PreferencesUtils.VALUE_INSTRUCTION_READ);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater
				.inflate(R.layout.location_instruction, container, false);
	}

	class ImageAdapter extends BaseAdapter {
		Context context;

		public ImageAdapter(Context ctx) {
			this.context = ctx;

		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mPicture.length + 1;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ImageView imageView = new ImageView(context);
			LinearLayout layout = new LinearLayout(context);
			layout.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			layout.setGravity(Gravity.TOP);
			layout.addView(imageView);
			int height = (int) (700 * HotauActivity.galleryImageRate);
			imageView.setLayoutParams(new LinearLayout.LayoutParams(
					LayoutParams.WRAP_CONTENT, height));
			imageView.setScaleType(ImageView.ScaleType.FIT_XY);
			imageView.setBackgroundResource(mPicture[position % 3]);
			return layout;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
		if (position >= 3) {
			HotauActivity togetherActivity = (HotauActivity) getActivity();
			togetherActivity.pushFragment("", new TogetherFragment());
		}

	}

	@Override
	public void onItemSelected(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
		if (position >= 3) {
			HotauActivity togetherActivity = (HotauActivity) getActivity();
			togetherActivity.pushFragment("", new TogetherFragment());
		}
	}

	@Override
	public void onNothingSelected(AdapterView<?> parent) {
		// TODO Auto-generated method stub

	}

}
